<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/12/28
 * Time: 09:21
 */

namespace Admin\Controller;
use Admin\Api\DatatreeApi;
use Admin\Api\MemberApi;
use Shop\Api\RedEnvelopeApi;
use Shop\Api\RedEnvelopeTplApi;
use Shop\Model\RedEnvelopeModel;

/**
 * 红包模版
 *
 * Class RedEnvelopeTplController
 * @author 老胖子-何必都 <hebiduhebi@126.com>
 * @date 20151228
 * @package Admin\Controller
 */
class RedEnvelopeTplController extends AdminController {


    public function index(){
        if(IS_GET){
            $map = array();
            $page = array('curpage'=>1,'size'=>20);
            $result = apiCall(RedEnvelopeTplApi::QUERY,array($map,$page,'create_time desc'));
            if($result['status']){
                $this->assign('list',$result['info']['list']);
            }
            $this->display();
        }else{

        }
    }

    public function giveRedEnvelope(){
        $id = $this->_get('id','','缺少红包模版ID');
        if(IS_GET){
            $this->assign('id',$id);
            $result = apiCall(RedEnvelopeTplApi::GET_INFO,array(array('id'=>$id)));
            $map = array(
            );
            if($result['status'] && is_array($result['info'])){
                $map['dtree_type'] = $result['info']['dtree_type'];
            }
            $page = array('curpage'=>I('param.p',0),'size'=>10);
            $result = apiCall(RedEnvelopeApi::QUERY,array($map,$page,'get_time desc'));
            if($result['status']){
                $this->assign('list',$result['info']['list']);
                $this->assign('show',$result['info']['show']);
            }
            $this->display();
        }else{
            $uid = $this->_post('uid','','缺少用户ID');
            $notes = $this->_post('notes','','备注缺失');

            $money = 0.0;
            $expire_time = 0;

            $result = apiCall(RedEnvelopeTplApi::GET_INFO,array(array('id'=>$id)));
            if($result['status']){
                if($result['info']['dtree_type'] == getDatatree('RED_TEN_PERCENT')){
                    $this -> error('不支持此类型红包');
                }
                $money = $result['info']['money'];
                $expire_time = $result['info']['expire_time'];
            }else{
                $this -> error($result['info']);
            }

            $entity = array(
                'notes'=>$notes,
                'uid'=>$uid,
                'get_time'=>time(),
                'use_status'=> RedEnvelopeModel::NOT_USE,
                'tpl_id'=>$id,
                'money' => $money,
                'expire_time' => $expire_time,
            );
            $result = apiCall(RedEnvelopeApi::ADD,array($entity));
            if($result['status']){
                $this->success("发送成功!");
            }else{
                $this->error($result['info']);
            }
        }

    }

    public function edit(){

        $id = $this->_get('id','','缺少红包模版ID');
        if(IS_POST){
            $dtree_type = $this->_post('dtree_type',0,'红包类别必须');
            $use_condition = $this->_post('use_condition','','红包使用条件必须');
            $expire_time = $this->_post('expire_time','','红包过期时间必须');
            $expire_time = intval($expire_time);
            $expire_time = $expire_time * 24*3600;
            if($expire_time <= 0){
                $expire_time = 100*365*24*3600;//约100年
            }
            $money = $this->_post('money','','红包过期时间必须');
            $notes = $this->_post('notes','');
            $entity = array(
                'dtree_type'=>$dtree_type,
                'use_condition'=>$use_condition,
                'expire_time'=>$expire_time,
                'money'=>$money,
                'uid'=>UID,
                'notes'=>$notes,
            );

            $result = apiCall(RedEnvelopeTplApi::SAVE_BY_ID,array($id,$entity));
            if($result['status']){
                $this->success("保存成功",U('Admin/RedEnvelopeTpl/index'));
            }else{
                $this->error($result['info']);
            }

        }else{
            $result = apiCall(RedEnvelopeTplApi::GET_INFO,array(array('id'=>$id)));
            $this -> assign('red_envolope_type',getDatatree('RED_ENVOLOPE_TYPE'));
            if($result['status'])
            {
                $this->assign('vo',$result['info']);
            }
            $this->display();
        }
    }

    public function add(){
        if(IS_POST){
            $dtree_type = $this->_post('dtree_type',0,'红包类别必须');
            $use_condition = $this->_post('use_condition','','红包使用条件必须');
            $expire_time = $this->_post('expire_time','','红包过期时间必须');
            $expire_time = intval($expire_time);
            $expire_time = $expire_time * 24*3600;
            if($expire_time <= 0){
                $expire_time = 100*365*24*3600;//约100年
            }
            $money = $this->_post('money','','红包过期时间必须');
            $notes = $this->_post('notes','');
            $entity = array(
                'dtree_type'=>$dtree_type,
                'status'=>1,
                'use_condition'=>$use_condition,
                'expire_time'=>$expire_time,
                'money'=>$money,
                'create_time'=>time(),
                'uid'=>UID,
                'notes'=>$notes,
            );

            $result = apiCall(RedEnvelopeTplApi::ADD,array($entity));
            if($result['status']){
                $this->success("添加成功",U('Admin/RedEnvelopeTpl/index'));
            }else{
                $this->error($result['info']);
            }

        }else{
            $this -> assign('red_envolope_type',getDatatree('RED_ENVOLOPE_TYPE'));
            $this->display();
        }
    }


    /**
     * 启用/批量启用
     * id,ids
     */
    public function enable() {
        $id = $this->_get('id','','缺少ID参数');
        $map = array('id' => $id);

        $result = apiCall(RedEnvelopeTplApi::ENABLE, array($map));

        if ($result['status']) {
            $this -> success("启用成功！");
        } else {
            $this -> error($result['info']);
        }
    }

    /**
     * 禁用/批量禁用
     * id,ids
     */
    public function disable() {
        $id = $this->_get('id','','缺少ID参数');
        $map = array('id' => $id);

        $result = apiCall(RedEnvelopeTplApi::DISABLE, array($map));

        if ($result['status']) {
            $this -> success("禁用成功！");
        } else {
            $this -> error($result['info']);
        }
    }

    public function users(){

        $map['nickname'] = array('like', "%" . I('q', '', 'trim') . "%");
        $map['uid'] = I('q', -1);
        $map['_logic'] = 'OR';
        $page = array('curpage' => 0, 'size' => 20);
        $order = " last_login_time desc ";

        $result = apiCall(MemberApi::QUERY, array($map, $page, $order, false, 'uid,nickname,head'));

        if ($result['status']) {
            $list = $result['info']['list'];
            foreach($list as &$vo){
                $vo['head'] = getImageUrl($vo['head'],80);
                $vo['id'] = $vo['uid'];
            }
            $this -> success($list);
        } else {
            LogRecord($result['info'], __LINE__);
        }
    }

}